using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Utility;
using Entities;

public partial class Admin_Danhsachamthuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            LoadData();

        }
    }
    private void LoadData()
    {
        pager.PageSize = 5;
        Criteria cri = new Criteria();
        int total = 0;
        if (txttenat!= null)
            cri.add(Condition.AND, tbAmthucCollumn .tenamthuc .ToString(), Condition.LIKE, txttenat.Text);

        tbAmthucCollection  atCollection = tbAmthucBUS .Get(cri.Criter, pager.CurrentIndex, pager.PageSize,
       tbAmthucCollumn .maamthuc .ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = atCollection;
        Repeater1.DataBind();

        pager.ItemCount = total;

    }

    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = Convert.ToInt32(e.CommandArgument);
        LoadData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item
        || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            tbAmthuc  athuc = (tbAmthuc )e.Item.DataItem;


           

           HyperLink  hp_tenat = (HyperLink )e.Item.FindControl("hp_tenat");
           hp_tenat.Text = athuc.Tenamthuc;
           hp_tenat.NavigateUrl = "~/Admin/Danhsachdiadanh.aspx?id=" + athuc.Maamthuc.ToString();

            Literal lit_mota = (Literal)e.Item.FindControl("lit_mota");
            lit_mota.Text = athuc.Mota ;


            HyperLink hp_sua = (HyperLink)e.Item.FindControl("hp_sua");
            hp_sua.NavigateUrl = "~/Admin/Suaamthuc.aspx?id=" + athuc.Maamthuc .ToString();

            Image img_at = (Image)e.Item.FindControl("img_at");
            img_at.ImageUrl = "~/Hinhanh/Hinh/" + athuc.Hinhanh ;




        }
    }
    protected void lbt_xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_xoa");
            if (cb_xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbAmthuc  athuc = tbAmthucBUS .GetByID(id);

                if (athuc.Hinhanh  != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/" + athuc.Hinhanh ));
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/Hinh/" + athuc.Hinhanh ));
                }
                tbAmthucBUS .Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}
